package com.summer.framework.core.lock;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.summer.framework.base.lock.LockService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description:
 *
 * @auther Ice Yang
 * Date 2025/9/18 11:40
 */
@Component
@ConditionalOnMissingBean(LockService.class)
public class ReentrantLockService implements LockService {

    private static final Cache<String, ReentrantLock> cache = Caffeine.newBuilder()
            .expireAfterAccess(60, TimeUnit.MINUTES)
            .maximumSize(100)
            .initialCapacity(50)
            .recordStats()
            .build();

    @Override
    public Lock getLock(String key) {
        return cache.get(key, k -> new ReentrantLock());
    }
}
